package com.zyl.blog.common.service;

import com.zyl.blog.common.utils.Extreme.JsonUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @ProjectName: Personal_Blog
 * @Package: com.zyl.common.service
 * @ClassName: LogMaskService
 * @version v1.0
 * @Description
 * @Author FinalFantasy
 * @Date 2025/2/17-21:21
 */
@Component
public class LogMaskService {
    private static final Pattern MOBILE_PATTERN = Pattern.compile("(\\d{3})\\d{4}(\\d{4})");
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("(\\d{4})\\d{10}(\\w{4})");

    public String maskParams(Object[] args) {
        return Arrays.stream(args)
                .map(this::maskObject)
                .collect(Collectors.joining(", "));
    }

    public String maskResult(Object result) {
        return maskObject(result);
    }

    private String maskObject(Object obj) {
        String json = JsonUtils.toJson(obj);
        // 手机号脱敏
        json = MOBILE_PATTERN.matcher(json).replaceAll("$1****$2");
        // 身份证脱敏
        json = ID_CARD_PATTERN.matcher(json).replaceAll("$1**********$2");
        return json;
    }
}